#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@UserFor     :   ***    ***
@File        :   push_search_keywords.py    
@Email       :   18574945291@163.com
@Modify Time :   2020/12/11 20:30
@Author      :   code_wizard (wll) 
"""

import sys, os
import pymysql, redis
import umsgpack

sys.path.append(os.getcwd())
import local_settings


class BaseInsert(object):
    def __init__(self):
        self.connect = pymysql.connect(**local_settings.mysql_dev_ml)
        self.cursor = self.connect.cursor(cursor=pymysql.cursors.DictCursor)
        self.redis_con = redis.Redis(**local_settings.redis_dev)

    def push_keys_to_redis(self, *args):
        for index in args:
            if index is None:
                continue
            self.redis_con.lpush(local_settings.COOKIE_KEYWORDS_NAME, umsgpack.packb(index))
            print("push success : keys = : \t{key}  ! ".format(key=index))

    def push_key_to_pool(self, key):
        if key:
            self.redis_con.lpush(local_settings.COOKIE_KEYWORDS_NAME, umsgpack.packb(key.strip()))

    def pop_key(self):
        res = self.redis_con.lpop(local_settings.COOKIE_KEYWORDS_NAME)
        if res:
            return umsgpack.unpackb(res)

    def __del__(self):
        if self.connect is not None:
            self.connect.commit
            self.connect.close
        if self.redis_con is not None:
            self.redis_con.close()


class PushKey(BaseInsert):
    """
    只要重写这个方法 返回关键字列表就可以了
    """
    def get_keywords_list(self):
        self.cursor.execute("select distinct(company_name) from chsell_order where company_name is not null")
        result = self.cursor.fetchall()
        key_list = list()
        for i in result:
            if i is not None and len(i.get("company_name",None))>5:
                full_name = i.get("company_name",None).strip()
                print(full_name)
                self.push_key_to_pool(full_name)
                key_list.append(i.get("company_name",None))
        # self.push_key_to_redis(*key_list)



if __name__ == '__main__':
    push = PushKey()
    push.get_keywords_list()
    # print(push.pop_key())
